package com.shiguiwu.springmybatis.mybatis.spring;

import com.shiguiwu.springmybatis.mybatis.Book;
import com.shiguiwu.springmybatis.mybatis.mapper.BookMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import javax.sql.DataSource;
import java.util.Arrays;
import java.util.List;

/**
 * 两种方法都可以
 * @description: mybatis和spring整合，注解版
 * @author: stone
 * @date: Created by 2022/1/16 13:18
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.mybatis.spring
 *
 * 注释掉了，跑整个项目会影响的
 */
//@MapperScan("com.shiguiwu.springmybatis.mybatis.mapper")
//@ComponentScan("com.shiguiwu.springmybatis.mybatis")
//@Configuration
public class MybatisSpringTests {

    //@Bean
    //@ConditionalOnMissingBean
    public  DataSource dataSource() {
        DataSourceBuilder<?> dataSourceBuilder = DataSourceBuilder.create();
        dataSourceBuilder.driverClassName("com.mysql.cj.jdbc.Driver");
        dataSourceBuilder.url("jdbc:mysql://127.0.0.1:3306/lunwen?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai");
        dataSourceBuilder.username("root");
        dataSourceBuilder.password("123456");

        return dataSourceBuilder.build();
    }



    //@Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
        Resource resource = resourcePatternResolver.getResource("classpath:mybatis-config.xml");
        sqlSessionFactoryBean.setConfigLocation(resource);
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean.getObject();
    }

    //@Bean
    //@ConditionalOnMissingBean
    public SqlSessionFactory sqlSessionFactory_v2() throws Exception {
        return new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
    }




    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MybatisSpringTests.class);

        String[] names = applicationContext.getBeanDefinitionNames();

        Arrays.stream(names).forEach(System.out::println);
        BookMapper bookMapper = applicationContext.getBean(BookMapper.class);

        List<Book> bookList = bookMapper.getBookList();

        System.out.println(bookList);
    }

}
